<template>
    <div class="publicpageview">
        <div class="publicformview">
            <el-form :inline="true" :model="listQuery" size="mini" ref="searchForm">
                <el-row :gutter="20">
                    <el-col :span="4">
                        <el-form-item label="账户名称：" prop="keyword">
                            <el-input v-model="listQuery.keyword"></el-input>
                        </el-form-item>
                    </el-col>

                    <el-col :span="4">
                        <el-form-item label="流水类型：" prop="status">
                            <el-select v-model="listQuery.status" clearable>
                                <el-option label="启用" value="0" key="1"></el-option>
                                <el-option label="禁用" value="1" key="0"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>

                    <el-col :span="4">
                        <el-form-item label="流水日期：" prop="name">
                            <el-date-picker v-model="listQuery.projectYear" type="date" placeholder="年/月/日">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>

                    <el-col :span="4">
                        <el-form-item label="发生金额：" prop="keyword">
                            <el-input v-model="listQuery.keyword"></el-input>
                        </el-form-item>
                    </el-col>

                    <el-col :span="4">
                        <el-form-item label="摘要：" prop="keyword">
                            <el-input v-model="listQuery.keyword"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="4">
                        <div>
                            <el-button size="mini" type="primary" @click="handleSearch">查询</el-button>
                            <el-button size="mini" @click="resetForm('searchForm')">重置</el-button>
                        </div>
                    </el-col>
                </el-row>
            </el-form>
        </div>

        <div class="publitableview">
            <el-button type="primary" size="mini" @click="handleAddUser('add')">新增收入</el-button>
            <el-table :data="list" border fit highlight-current-row size="mini">
                <el-table-column align="center" label="账户名称" prop="id">
                </el-table-column>


                <el-table-column align="center" label="流水类型" prop="status">
                    <template slot-scope="scope">
                        <span>{{ scope.row.status == 0 ? "禁用" : "启用" }}</span>
                    </template>
                </el-table-column>

                <el-table-column align="center" label="发生日期" prop="name">
                </el-table-column>

                <el-table-column align="center" label="发生金额" prop="name">
                </el-table-column>

                <el-table-column align="center" label="摘要" prop="name">
                </el-table-column>

                <el-table-column align="center" label="当前余额" prop="name">
                </el-table-column>


                <el-table-column align="center" label="操作" width="180">
                    <template slot-scope="scope">
                        <el-button type="text" size="mini" @click="handleUpdate(scope.row, 'update')">修改</el-button>
                        <el-button type="text" size="mini" @click="handleUpdate(scope.row,'details')">详情</el-button>
                        <el-button type="text" size="mini" @click="handleDelete(scope.row.id)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="publicpaginationview">
                <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                    :current-page="listQuery.pageNum" background :page-sizes="[10, 20, 50, 100]"
                    :page-size="listQuery.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total"
                    small></el-pagination>
            </div>
        </div>

        <el-dialog :title="isAdd == 'add' ? '新增收入' : isAdd == 'update' ? '修改收入' :'收入详情'" :visible.sync="dialogVisible"
            width="40%" :before-close="handleClose">
            <el-form :model="formData" ref="dataForm" class="demo-ruleForm" label-position="right" label-width="120px"
                size="mini">
                <el-form-item label="账户名称：" prop="id">
                    <el-input v-model="formData.id"></el-input>
                </el-form-item>
                <el-form-item label="账号：" prop="name">
                    <el-input v-model="formData.name"></el-input>
                </el-form-item>


                <el-form-item label="流水类型：" prop="status">
                    <el-select v-model="formData.status">
                        <el-option label="启用" value="1" key="1"></el-option>
                        <el-option label="禁用" value="0" key="0"></el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="期初余额：" prop="name">
                    <el-input v-model="formData.name"></el-input>
                </el-form-item>

                <el-form-item label="发生日期：" prop="name">
                    <el-date-picker v-model="formData.projectYear" type="date" placeholder="年/月/日">
                    </el-date-picker>
                </el-form-item>

                <el-form-item label="发生金额：" prop="name">
                    <el-input v-model="formData.name"></el-input>
                </el-form-item>
                <el-form-item label="备注：" prop="remarks">
                    <el-input v-model="formData.remarks" type="textarea" :rows="2"></el-input>
                </el-form-item>

                <el-form-item label="附件列表：">
                    <el-button type="text" @click="handleBatchImport">上传附件</el-button>
                </el-form-item>

                <div style="padding: 5px 40px;" v-for="(item, index) in showurlList">{{item.name}} <i style="margin-left: 30px;" class="el-icon-close" @click="handleDeleteShowList(index)"></i></div>
            </el-form>



            <span slot="footer" class="dialog-footer">
                <el-button type="primary" @click="handleClose" size="medium" v-if="isAdd == 'details'">返
                    回</el-button>

                <el-button type="primary" @click="handleSubmitClick" size="medium" v-else>保 存</el-button>
            </span>
        </el-dialog>

        <el-dialog title="上传文件" :visible.sync="uploadDialogVisible" width="415px" @close="handleUploadDialogClose">
            <div class="upload-container">
                <el-upload class="upload-dragger" drag :action="uploadAction" :file-list="fileList"
                    :on-success="handleUploadSuccess" :on-error="handleUploadError" :on-remove="handleFileRemove"
                    :before-upload="beforeUpload" :auto-upload="true" ref="upload">
                    <i class="el-icon-upload"></i>
                    <div class="el-upload__text">
                        请将文件拖动到此处，或<em style="color: #008C93;">点击上传</em>
                    </div>
                </el-upload>
            </div>

            <div slot="footer" class="dialog-footer">

                <el-button type="primary" size="mini" @click="handleImport">保 存</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
let arr = [];
let treeArr = [];
import { listAll, deleteRole, createRole, allResourceRole, updateRole } from '@/api/system'
import { listResource, allocResource } from '@/api/index'
import { Message } from 'element-ui'
export default {
    name: "AccommodationManagementIndex",

    data() {
        return {
            listQuery: {
                pageNum: 1,
                pageSize: 10,

            },
            total: 0,
            list: [],
            dialogVisible: false,
            isAdd: 'add',
            formData: {},
            uploadDialogVisible: false,
            fileList: [],
            uploadAction: 'http://125.39.77.18:9021/mall-admin/fileUpload/upload',
            urlList: [],
            showurlList: []
        };
    },

    computed: {

    },

    mounted() {
        this.getList();
    },



    methods: {

        handleDeleteShowList(index) {
            this.showurlList.splice(index, 1)
         },
        // 上传失败
        handleUploadError(err, file) {
            this.$message.error('文件上传失败');
        },
        // 导入数据
        handleImport() {
            if (this.fileList.length === 0) {
                this.$message.warning('请先选择要导入的文件');
                return;
            }
            if (this.urlList.length > 0) {
                this.urlList.forEach(item => {
                    this.showurlList.push({ url: item.url, name: item.name })
                })
            }

            this.uploadDialogVisible = false;
            this.$message.success('导入成功！');

        },
        // 批量导入
        handleBatchImport() {
            this.uploadDialogVisible = true;
            this.fileList = [];
            this.urlList = [];
        },

        // 上传前验证
        beforeUpload(file) {
            const isExcel = file.type === 'application/vnd.ms-excel' ||
                file.type === 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
            if (!isExcel) {
                this.$message.error('只能上传 Excel 文件！');
                return false;
            }

            const isLt2M = file.size / 1024 / 1024 < 2;
            if (!isLt2M) {
                this.$message.error('上传文件大小不能超过 2MB!');
                return false;
            }

            return true;
        },
        // 文件选择变化
        handleFileRemove(file, fileList) {
            this.fileList = fileList;
            let arr = [...this.urlList];
            let i;

            arr.forEach((item, index) => {
                if (file.response) {
                    if (item.url == file.response.data.url) {
                        i = index
                    }
                } else {
                    if (item.url == file.url) {
                        i = index
                    }
                }
            })

            if (i != undefined) {
                arr.splice(i, 1);
                this.urlList = arr;
            }


        },
        // 上传成功
        handleUploadSuccess(response, file, fileList) {

            if (response.code == 200) {
                this.fileList = fileList
                this.$message.success('文件上传成功');
                this.urlList.push(response.data);
            } else {
                this.$message.error('文件上传失败');
                let arr = [...fileList.map(i => i)]
                arr.pop();
                this.fileList = arr;
            }
        },
        handleUploadDialogClose() {
            this.fileList = [];
            this.urlList = [];
            this.$refs.upload.clearFiles();
            this.uploadDialogVisible = false;
        },



        handleSubmitClick() {
            const { formData } = this;
            if (this.isAdd == 'add') {
                createRole({ ...formData }).then(res => {
                    this.handleClose();
                    this.getList();
                    Message({
                        message: res.message || '操作成功',
                        type: "success",
                        duration: 5 * 1000,
                    });
                })
            }
            if (this.isAdd == 'update' || this.isAdd == 'details') {
                updateRole({ ...formData }).then(res => {
                    this.handleClose();
                    this.getList();
                    Message({
                        message: res.message || '操作成功',
                        type: "success",
                        duration: 5 * 1000,
                    });
                })
            }

        },


        handleSizeChange(val) {
            this.listQuery.pageNum = 1;
            this.listQuery.pageSize = val;
            this.getList();
        },
        handleCurrentChange(val) {
            this.listQuery.pageNum = val;
            this.getList();
        },

        handleSearch() {
            this.listQuery.pageNum = 1;
            this.getList();
        },
        getList() {
            listAll(this.listQuery).then((response) => {
                this.list = response.data.list;
                this.total = response.data.total;
            });
        },



        resetForm(val) {

            this.$refs[val].resetFields();
            this.listQuery.pageNum = 1;
        },

        handleAddUser(type) {
            this.isAdd = 'add';
            this.dialogVisible = true;

        },

        handleClose() {
            this.resetForm("dataForm");
            this.dialogVisible = false;
            this.showurlList = [];
        },

        handleUpdate(currentData, type) {
            this.isAdd = type;
            //   一定要在打开弹窗前 再赋值 不然resetFiles没用
            this.dialogVisible = true;
            this.$nextTick(() => {
                this.formData = { ...currentData, status: String(currentData.status) };

            });
        },

        handleDelete(id) {
            this.$confirm(
                "此操作将删除该条内容，请确认是否继续操作？",
                "删除提示",
                {
                    confirmButtonText: "保 存",
                    cancelButtonText: "取 消",
                    type: "warning",
                }
            )
                .then(() => {
                    deleteRole(id).then(res => {
                        Message({
                            message: res.message || '操作成功',
                            type: "success",
                            duration: 5 * 1000,
                        });
                        this.getList();
                    })
                })
                .catch(() => { });
        }
    },
};
</script>

<style scoped>
.el-date-editor.el-input,
.el-date-editor.el-input__inner {
    width: 100%;
}
</style>